﻿using DotNetty.Transport.Channels;
using InkjetControl.bean;
using InkjetControl.codec;
using InkjetControl.events;
using InkjetControl.NLog;
using Newtonsoft.Json;

namespace InkjetControl.services.resolvers
{
    public class RequestMessageResolver : IResolver
    {
        public Message Resolve(Message message)
        {
            Log.Logger.Debug("Srv Request Resolve: [ sessionID:{0}], mc:{1}, ml:{2}, recv body:{3}", message.SessionId, message.MachineCode, message.MachineLabel, message.Body);

            // 处理完成后，生成一个响应消息返回
           /* IBody responseBody = ProcessMessage(message);

            Message responseMsg = new Message()
            {
                SessionId = message.SessionId,
                MessageType = MessageType.RESPONSE,
                MachineCode = message.MachineCode,
                Body = responseBody
            };*/

            return ProcessMessage(message);
        }

        public bool Support(Message message)
        {
            return MessageType.REQUEST == message.MessageType;
        }

        private Message ProcessMessage(Message msg)
        {
            Message responseMsg = new Message()
            {
                SessionId = msg.SessionId,
                MessageType = MessageType.RESPONSE,
                MachineCode = msg.MachineCode,
                MachineLabel = msg.MachineLabel,
            };
            Body<object> msgBody = (Body<object>)msg.Body;
            if (msg.Body == null)
            {
                string info = "Error: unknown command";
                responseMsg.Body = Message.GenerateMsgBody(NetCmd.UNKNOWN_CMD, data: ref info);
                return responseMsg;
            }

            switch (msgBody.Cmd)
            {
                case NetCmd.START_SUPPLY_INK:
                    break;
                case NetCmd.STOP_SUPPLY_INK:
                    break;
                case NetCmd.START_PRINT:
                    break;
                case NetCmd.STOP_PRINT:
                    break;
                case NetCmd.REFRESH_STATUS:
                    StatusBean status;
                    // Log.Logger.Info("status :{0}", (msgBody.Data as string));
                    status = JsonConvert.DeserializeObject<StatusBean>(msgBody.Data?.ToString());
                    EventBus.OnStatusRefresh(msg.MachineCode, status, msg.MachineLabel);
                    responseMsg.MessageType = MessageType.EMPTY;
                    break;
                case NetCmd.GET_PRINT_PARAMS:
                    break;
                case NetCmd.SET_PRINT_PARAMS:
                    break;
                case NetCmd.GET_SYSTEM_PARAMS:
                    break;
                case NetCmd.SET_SYSTEM_PARAMS:
                    break;
                case NetCmd.CONNECTION:
                    break;

                case NetCmd.LOGIN:
                    bool success = true;
                    responseMsg.Body = Message.GenerateMsgBody(NetCmd.LOGIN, ref success);
                    break;
                default:
                    break;
            }

            return responseMsg;
        }
    }
}
